package top.huanyv.tools.jwt;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author huanyv
 * @date 2023/1/31 16:37
 */
public class Payload {

    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 发行人
     */
    private String iss;
    /**
     * 过期时间
     */
    private long exp;
    /**
     * 主题
     */
    private String sub;
    /**
     * 用户
     */
    private String aud;
    /**
     * 该时间前不可用
     */
    private long nbf;
    /**
     * 签发时间
     */
    private long iat;
    /**
     * jwt的唯一身份标识，主要用来作为一次性token,从而回避重放攻击。
     */
    private String jti;

    public Payload() {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
    }

    public Payload(String sub) {
        this();
        this.sub = sub;
    }

    public String getIss() {
        return iss;
    }

    public void setIss(String iss) {
        this.iss = iss;
    }

    public long getExp() {
        return exp;
    }

    public void setExp(long exp) {
        this.exp = exp;
    }

    public String getSub() {
        return sub;
    }

    public void setSub(String sub) {
        this.sub = sub;
    }

    public String getAud() {
        return aud;
    }

    public void setAud(String aud) {
        this.aud = aud;
    }

    public long getNbf() {
        return nbf;
    }

    public void setNbf(long nbf) {
        this.nbf = nbf;
    }

    public long getIat() {
        return iat;
    }

    public void setIat(long iat) {
        this.iat = iat;
    }

    public String getJti() {
        return jti;
    }

    public void setJti(String jti) {
        this.jti = jti;
    }

    @Override
    public String toString() {
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
